<?php
namespace app\index\controller;

use app\common\model\ZhCate as ZhCateModel;

/**
 * 分类控制器
 * Class Index
 * @package app\index\controller
 */
class Zhcate extends Base
{
    /**
     * [index  分类数据]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $user = $this->user;
        $model = new ZhCateModel;
        $map = [];
        try{
            $class = config('oneclass');
            foreach($class as &$v){
                $map['cid'] = $v['id'];
                $map['comid'] = $user['comid'];
                $map['states'] = ['neq',3];
                $cate = $model->getAllData($map);
                if($v['id'] == 1 || $v['id'] == 4){
                    $v['cate'] = array_merge($v['cate'],$cate);
                }else{
                    $v['cate'] = $cate;
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看内容分类列表');
        return admin_json(1,'获取成功',$class);
    }
    /**
     * [add  添加分类]
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new ZhCateModel;
        try{
            $param['comid'] = $user['comid'];
            $model->addData($param);
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'内容分类添加成功');
        return admin_json(1,'添加成功');
    }
    /**
     * [edit  编辑分类]
     * @return json
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new ZhCateModel;
        try{
            $model->editData($param);
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'内容分类编辑成功');
        return admin_json(1,'编辑成功');
    }
    /**
     * [del  删除分类]
     * @return json
     * @author [C。]
     */
    public function del()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new ZhCateModel;
        try{
            $model->editData(['id'=>$param['id'],'states'=>3]);
        }catch (\Exception $e) {
            writelog($user['mid'],$user['comid'],$e->getMessage());
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'内容分类删除成功');
        return admin_json(1,'删除成功');
    }
}